<template>
  <div class="app-container">
     <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:plating:add']"
          size="small"
        >新增</el-button>
      </el-col>
     </el-row>

    <el-table v-loading="loading" :data="platingList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="产品ID" align="center" prop="pid" />
      <el-table-column label="产品类型" align="center" prop="ptype" /> -->
      <el-table-column label="料号" align="center" prop="partNumber" />
      <el-table-column label="零件名称" align="center" prop="partName" />
      <el-table-column label="材质" align="center" prop="material" />
      <el-table-column label="用量 pcs" align="center" prop="qtyPcs" />
      <el-table-column label="单价 (RMB/PC)" align="center" prop="unitPrice" />
      <el-table-column label="废材单价 (RMB/KG)" align="center" prop="scrapUnitPrice" />
      <el-table-column label="料厚 (mm)" align="center" prop="thicknessMm" />
      <el-table-column label="料宽 (mm)" align="center" prop="widthMm" />
      <el-table-column label="PITCH (mm)" align="center" prop="pitchMm" />
      <el-table-column label="密度 (g/cm³)" align="center" prop="densityGCm3" />
      <el-table-column label="净重 (g)" align="center" prop="netWeightG" />
      <el-table-column label="损耗率 (%)" align="center" prop="lossRatePct" />
      <el-table-column label="不良率 (%)" align="center" prop="defectRatePct" />
      <el-table-column label="其他" align="center" prop="otherRemark" />
      <el-table-column label="管理费比例 (%)" align="center" prop="mgmtRatioPct" />
      <el-table-column label="利润比例 (%)" align="center" prop="profitRatioPct" />
      <el-table-column label="辅材费用 (RMB/PC)" align="center" prop="auxMaterialCost" />
      <el-table-column label="废材回收率 (%)" align="center" prop="scrapRecycleRatePct" />
      <el-table-column label="冲制数 (PC/次)" align="center" prop="punchesPerShot" />
      <el-table-column label="耗料 (KG)" align="center" prop="materialConsumptionKg" />
      <el-table-column label="不良费 (RMB/PC)" align="center" prop="defectFee" />
      <el-table-column label="材料成本 (RMB/PC)" align="center" prop="materialCost" />
      <el-table-column label="管理费 (RMB/PC)" align="center" prop="mgmtFee" />
      <el-table-column label="利润 (RMB/PC)" align="center" prop="profitFee" />
      <el-table-column label="材料成本合计 (RMB)" align="center" prop="totalMaterialCost" />
      <el-table-column label="机台型号及吨位" align="center" prop="machineModelTonnage" />
      <el-table-column label="成本率 (%)" align="center" prop="costRatePct" />
      <el-table-column label="冲速 (SPM)" align="center" prop="punchSpeedSpm" />
      <el-table-column label="分/时" align="center" prop="minutePerHour" />
      <el-table-column label="效率 (%)" align="center" prop="efficiencyPct" />
      <el-table-column label="模具维修天数" align="center" prop="moldRepairDays" />
      <el-table-column label="模具维修费用 (RMB)" align="center" prop="moldRepairCost" />
      <el-table-column label="二次加工费 (RMB)" align="center" prop="secondaryProcessFee" />
      <el-table-column label="其他 (加工)" align="center" prop="otherProcessRemark" />
      <el-table-column label="加工管理费占比 (%)" align="center" prop="processMgmtRatioPct" />
      <el-table-column label="加工利润占比 (%)" align="center" prop="processProfitRatioPct" />
      <el-table-column label="模具维修RMB/PCS" align="center" prop="moldRepairRmbPcs" />
      <el-table-column label="加工不良费 (RMB/PC)" align="center" prop="processDefectFee" />
      <el-table-column label="冲制成本 (RMB/PC)" align="center" prop="punchCost" />
      <el-table-column label="加工管理费 (RMB/PC)" align="center" prop="processMgmtFee" />
      <el-table-column label="加工利润 (RMB/PC)" align="center" prop="processProfitFee" />
      <el-table-column label="冲制件成本合计 (RMB)" align="center" prop="totalPunchCost" />
      <el-table-column label="工站名称" align="center" prop="stationName" />
      <el-table-column label="人力 (人)" align="center" prop="laborHeadcount" />
      <el-table-column label="周期 (S)" align="center" prop="cycleTimeS" />
      <el-table-column label="穴数 (PCS)" align="center" prop="cavitiesPcs" />
      <el-table-column label="工费率 (RMB/H)" align="center" prop="laborRateRmbH" />
      <el-table-column label="工时 (S)" align="center" prop="laborTimeS" />
      <el-table-column label="人工费 (RMB/PC)" align="center" prop="laborFee" />
      <el-table-column label="包装费 (RMB/PC)" align="center" prop="packingFee" />
      <el-table-column label="运输费 (RMB/PC)" align="center" prop="transportFee" />
      <el-table-column label="未电镀总成本 (RMB/PC)" align="center" prop="totalCostBeforePlating" />
      <el-table-column label="电镀后总成本 (RMB/PC)" align="center" prop="totalCostAfterPlating" />
      <el-table-column label="冲压产能 (PCS/H)" align="center" prop="punchCapacityPcsH" />
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:plating:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:plating:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
     <!-- 添加或修改冲压电镀件对话框 -->
      <div  :title="title" v-if="open" style="margin-top:25px">
        <!-- <div class="titleclass">{{ title }}</div> -->
      <el-form ref="platingRef" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="产品ID" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入产品ID" />
        </el-form-item> -->
          <el-row :gutter="20">
        <el-form-item label="料号" prop="partNumber">
          <el-input v-model="form.partNumber" placeholder="请输入料号" />
        </el-form-item>
        <el-form-item label="零件名称" prop="partName">
          <el-input v-model="form.partName" placeholder="请输入零件名称" />
        </el-form-item>
        <el-form-item label="材质" prop="material">
          <el-input v-model="form.material" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="用量 pcs" prop="qtyPcs">
          <el-input v-model="form.qtyPcs" placeholder="请输入用量 pcs" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="单价 (RMB/PC)" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="废材单价 (RMB/KG)" prop="scrapUnitPrice">
          <el-input v-model="form.scrapUnitPrice" placeholder="请输入废材单价 (RMB/KG)" />
        </el-form-item>
        <el-form-item label="料厚 (mm)" prop="thicknessMm">
          <el-input v-model="form.thicknessMm" placeholder="请输入料厚 (mm)" />
        </el-form-item>
        <el-form-item label="料宽 (mm)" prop="widthMm">
          <el-input v-model="form.widthMm" placeholder="请输入料宽 (mm)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="PITCH (mm)" prop="pitchMm">
          <el-input v-model="form.pitchMm" placeholder="请输入PITCH (mm)" />
        </el-form-item>
        <el-form-item label="密度 (g/cm³)" prop="densityGCm3">
          <el-input v-model="form.densityGCm3" placeholder="请输入密度 (g/cm³)" />
        </el-form-item>
        <el-form-item label="净重 (g)" prop="netWeightG">
          <el-input v-model="form.netWeightG" placeholder="请输入净重 (g)" />
        </el-form-item>
        <el-form-item label="损耗率 (%)" prop="lossRatePct">
          <el-input v-model="form.lossRatePct" placeholder="请输入损耗率 (%)" />
        </el-form-item>
        </el-row>

        <el-row :gutter="20">
        <el-form-item label="不良率 (%)" prop="defectRatePct">
          <el-input v-model="form.defectRatePct" placeholder="请输入不良率 (%)" />
        </el-form-item>
        <el-form-item label="其他" prop="otherRemark">
          <el-input v-model="form.otherRemark"  placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="管理费比例 (%)" prop="mgmtRatioPct">
          <el-input v-model="form.mgmtRatioPct" placeholder="请输入管理费比例 (%)" />
        </el-form-item>
        <el-form-item label="利润比例 (%)" prop="profitRatioPct">
          <el-input v-model="form.profitRatioPct" placeholder="请输入利润比例 (%)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="辅材费用 (RMB/PC)" prop="auxMaterialCost">
          <el-input v-model="form.auxMaterialCost" placeholder="请输入辅材费用 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="废材回收率 (%)" prop="scrapRecycleRatePct">
          <el-input v-model="form.scrapRecycleRatePct" placeholder="请输入废材回收率 (%)" />
        </el-form-item>
        <el-form-item label="冲制数 (PC/次)" prop="punchesPerShot">
          <el-input v-model="form.punchesPerShot" placeholder="请输入冲制数 (PC/次)" />
        </el-form-item>
        <el-form-item label="耗料 (KG)" prop="materialConsumptionKg">
          <el-input v-model="form.materialConsumptionKg" placeholder="请输入耗料 (KG)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="不良费 (RMB/PC)" prop="defectFee">
          <el-input v-model="form.defectFee" placeholder="请输入不良费 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="材料成本 (RMB/PC)" prop="materialCost">
          <el-input v-model="form.materialCost" placeholder="请输入材料成本 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="管理费 (RMB/PC)" prop="mgmtFee">
          <el-input v-model="form.mgmtFee" placeholder="请输入管理费 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="利润 (RMB/PC)" prop="profitFee">
          <el-input v-model="form.profitFee" placeholder="请输入利润 (RMB/PC)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="材料成本合计 (RMB)" prop="totalMaterialCost">
          <el-input v-model="form.totalMaterialCost" placeholder="请输入材料成本合计 (RMB)" />
        </el-form-item>
        <el-form-item label="机台型号及吨位" prop="machineModelTonnage">
          <el-input v-model="form.machineModelTonnage" placeholder="请输入机台型号及吨位" />
        </el-form-item>
        <el-form-item label="成本率 (%)" prop="costRatePct">
          <el-input v-model="form.costRatePct" placeholder="请输入成本率 (%)" />
        </el-form-item>
        <el-form-item label="冲速 (SPM)" prop="punchSpeedSpm">
          <el-input v-model="form.punchSpeedSpm" placeholder="请输入冲速 (SPM)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="分/时" prop="minutePerHour">
          <el-input v-model="form.minutePerHour" placeholder="请输入分/时" />
        </el-form-item>
        <el-form-item label="效率 (%)" prop="efficiencyPct">
          <el-input v-model="form.efficiencyPct" placeholder="请输入效率 (%)" />
        </el-form-item>
        <el-form-item label="模具维修天数" prop="moldRepairDays">
          <el-input v-model="form.moldRepairDays" placeholder="请输入模具维修天数" />
        </el-form-item>
        <el-form-item label="模具维修费用 (RMB)" prop="moldRepairCost">
          <el-input v-model="form.moldRepairCost" placeholder="请输入模具维修费用 (RMB)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="二次加工费 (RMB)" prop="secondaryProcessFee">
          <el-input v-model="form.secondaryProcessFee" placeholder="请输入二次加工费 (RMB)" />
        </el-form-item>
        <el-form-item label="其他 (加工)" prop="otherProcessRemark">
          <el-input v-model="form.otherProcessRemark" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="加工管理费占比 (%)" prop="processMgmtRatioPct">
          <el-input v-model="form.processMgmtRatioPct" placeholder="请输入加工管理费占比 (%)" />
        </el-form-item>
        <el-form-item label="加工利润占比 (%)" prop="processProfitRatioPct">
          <el-input v-model="form.processProfitRatioPct" placeholder="请输入加工利润占比 (%)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="模具维修RMB/PCS" prop="moldRepairRmbPcs">
          <el-input v-model="form.moldRepairRmbPcs" placeholder="请输入模具维修RMB/PCS" />
        </el-form-item>
        <el-form-item label="加工不良费 (RMB/PC)" prop="processDefectFee">
          <el-input v-model="form.processDefectFee" placeholder="请输入加工不良费 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="冲制成本 (RMB/PC)" prop="punchCost">
          <el-input v-model="form.punchCost" placeholder="请输入冲制成本 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="加工管理费 (RMB/PC)" prop="processMgmtFee">
          <el-input v-model="form.processMgmtFee" placeholder="请输入加工管理费 (RMB/PC)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="加工利润 (RMB/PC)" prop="processProfitFee">
          <el-input v-model="form.processProfitFee" placeholder="请输入加工利润 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="冲制件成本合计 (RMB)" prop="totalPunchCost">
          <el-input v-model="form.totalPunchCost" placeholder="请输入冲制件成本合计 (RMB)" />
        </el-form-item>
        <el-form-item label="工站名称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入工站名称" />
        </el-form-item>
        <el-form-item label="人力 (人)" prop="laborHeadcount">
          <el-input v-model="form.laborHeadcount" placeholder="请输入人力 (人)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="周期 (S)" prop="cycleTimeS">
          <el-input v-model="form.cycleTimeS" placeholder="请输入周期 (S)" />
        </el-form-item>
        <el-form-item label="穴数 (PCS)" prop="cavitiesPcs">
          <el-input v-model="form.cavitiesPcs" placeholder="请输入穴数 (PCS)" />
        </el-form-item>
        <el-form-item label="工费率 (RMB/H)" prop="laborRateRmbH">
          <el-input v-model="form.laborRateRmbH" placeholder="请输入工费率 (RMB/H)" />
        </el-form-item>
        <el-form-item label="工时 (S)" prop="laborTimeS">
          <el-input v-model="form.laborTimeS" placeholder="请输入工时 (S)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="人工费 (RMB/PC)" prop="laborFee">
          <el-input v-model="form.laborFee" placeholder="请输入人工费 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="包装费 (RMB/PC)" prop="packingFee">
          <el-input v-model="form.packingFee" placeholder="请输入包装费 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="运输费 (RMB/PC)" prop="transportFee">
          <el-input v-model="form.transportFee" placeholder="请输入运输费 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="未电镀总成本 (RMB/PC)" prop="totalCostBeforePlating">
          <el-input v-model="form.totalCostBeforePlating" placeholder="请输入未电镀总成本 (RMB/PC)" />
        </el-form-item>
                </el-row>

        <el-row :gutter="20">
        <el-form-item label="电镀后总成本 (RMB/PC)" prop="totalCostAfterPlating">
          <el-input v-model="form.totalCostAfterPlating" placeholder="请输入电镀后总成本 (RMB/PC)" />
        </el-form-item>
        <el-form-item label="冲压产能 (PCS/H)" prop="punchCapacityPcsH">
          <el-input v-model="form.punchCapacityPcsH" placeholder="请输入冲压产能 (PCS/H)" />
        </el-form-item>
                </el-row>

      
        <!-- <el-form-item label="全局唯一标识" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入全局唯一标识" />
        </el-form-item> -->
      </el-form>
     <div style="text-align: right;">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
   </div>










     <div class="calculate">
      <el-row :gutter="20">
         <el-col :span="4">
         耗料:    {{ isNaN(consumerMaterial)?"":consumerMaterial}}  
         </el-col>
         <el-col :span="20">
          计算公式： 料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000*(1+损耗率{{form.lossRatePct}}+不良率{{form.defectRatePct}})    =I2*H2*J2*K2/1000*(1+M2+N2)  
         </el-col>
      </el-row>
       <el-row :gutter="20">
         <el-col :span="4">
         不良费（RMB/PC）: {{ headLossRate }}
         </el-col>
         <el-col :span="20">
           计算公式：  料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000*不良率{{form.defectRatePct}}*(单价{{form.unitPrice}}-废材单价{{ form.scrapUnitPrice }}*废材回收率{{ form.scrapRecycleRatePct }})/1000+辅材费用{{form.auxMaterialCost}}*不良率{{ form.defectRatePct }} =I2*H2*J2*K2/1000*N2*(F2-G2*S2)/1000+R2*N2  
         </el-col>
        </el-row>

       <el-row :gutter="20">
         <el-col :span="4">
         材料成本: {{ auxiliaryCost }}
         </el-col>
         <el-col :span="20">
          计算公式：料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000*单价{{form.unitPrice}}/1000-(料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000-净重{{form.netWeightG}}*冲制数{{ form.punchesPerShot }})*废材回收率{{ form.scrapRecycleRatePct }}*废材单价{{ form.scrapUnitPrice }}/1000+料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000*损耗率{{form.lossRatePct}}*废材回收率{{ form.scrapRecycleRatePct }}*(单价{{form.unitPrice}}-废材单价{{ form.scrapUnitPrice }})/1000+辅材费用{{form.auxMaterialCost}}+不良费{{form.defectFee}}+其他{{ form.otherRemark }}       =I3*H3*J3*K3/1000*F3/1000-(I3*H3*J3*K3/1000-L3*T3)*S3*G3/1000+I3*H3*J3*K3/1000*M3*S3*(F3-G3)/1000+R3+V3+O3
         </el-col>
      </el-row>
      <el-row :gutter="20">
         <el-col :span="4">
         管理费（RMB/PC）: {{ materialTotalCost }}
         </el-col>
         <el-col :span="20">
          计算公式：材料成本{{form.materialCost}}*管理费比例{{ form.mgmtRatioPct }}  =W3*P3
         </el-col>
        </el-row>
            <el-row :gutter="20">
         <el-col :span="4">
         利润（RMB/PC）: {{ materialManagementFee }}
         </el-col>
         <el-col :span="20">
          计算公式：=(材料成本{{form.materialCost}}+管理费{{form.mgmtFee}})*利润比例{{ form.profitRatioPct }}   (W3+X3)*Q3
         </el-col> 
        </el-row>
         <el-row :gutter="20">
         <el-col :span="4">
         材料成本合计（RMB）: {{ materialManagementFee }}
         </el-col>
         <el-col :span="20">
          计算公式：(材料成本{{form.materialCost}}+管理费{{form.mgmtFee}}+利润{{ form.profitFee }})*用量pcs{{form.qtyPcs}}/冲制数{{ form.punchesPerShot }}   =(W2+X2+Y2)*E2/T2
         </el-col> 
        </el-row>
      </div>

  </div>



</template>

<script setup name="Plating">
import { listPlating, getPlating, delPlating, addPlating, updatePlating } from "@/api/system/plating"

const { proxy } = getCurrentInstance()
const platingList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    pid: null,
  },
  rules: {
    partNumber: [
      { required: true, message: "料号不能为空", trigger: "blur" }
    ],
    partName: [
      { required: true, message: "零件名称不能为空", trigger: "blur" }
    ],
  }
})
//耗料 计算公式： 料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000*(1+损耗率{{form.lossRatePct}}+不良率{{form.defectRatePct}})    =I2*H2*J2*K2/1000*(1+M2+N2)  
const materialConsumptionKg = computed(() => { 
    const form = data.form;
    const widthMm = Number(form.widthMm ?? 0);
    const thicknessMm = Number(form.thicknessMm ?? 0);
    const pitchMm = Number(form.pitchMm ?? 0) || 0; 
    const densityGCm3 = Number(form.densityGCm3 ?? 0);
    const defectRatePct = Number(form.defectRatePct ?? 0)/100;
    const lossRatePct = Number(form.lossRatePct ?? 0)/100;
    const tempconst = widthMm*thicknessMm*pitchMm*densityGCm3/1000;
    if (lossRatePct < 0 || lossRatePct > 100) {
      console.warn('损耗率必须在0到100之间');
      return 0;
    }
    if (defectRatePct < 0 || defectRatePct > 100) {
      console.warn('不良率必须在0到100之间');
      return 0;
    }
    return Number(
     tempconst*(1+lossRatePct+defectRatePct)
    ).toFixed(4);
})
watchEffect(() => {
  data.form.materialConsumptionKg = materialConsumptionKg.value;
});

//不良费   计算公式：  料宽{{form.widthMm}}*料厚{{form.thicknessMm}}*PITCH{{form.pitchMm}}*密度{{form.densityGCm3}}/1000*不良率{{form.defectRatePct}}*(单价{{form.unitPrice}}-废材单价{{ form.scrapUnitPrice }}
// *废材回收率{{ form.scrapRecycleRatePct }})/1000+辅材费用{{form.auxMaterialCost}}*不良率{{ form.defectRatePct }} =I2*H2*J2*K2/1000*N2*(F2-G2*S2)/1000+R2*N2

const defectFee = computed(() => { 
  const form = data.form;
    const widthMm = Number(form.widthMm ?? 0);
    const thicknessMm = Number(form.thicknessMm ?? 0);
    const pitchMm = Number(form.pitchMm ?? 0) || 0; 
    const densityGCm3 = Number(form.densityGCm3 ?? 0);

    const defectRatePct = Number(form.defectRatePct ?? 0)/100;
    const unitPrice = Number(form.unitPrice ?? 0);
    const scrapUnitPrice = Number(form.scrapUnitPrice ?? 0) || 0; 
    const scrapRecycleRatePct = Number(form.scrapRecycleRatePct ?? 0);

    const auxMaterialCost = Number(form.auxMaterialCost ?? 0);
      if (defectRatePct < 0 || defectRatePct > 100) {
      console.warn('不良率必须在0到100之间');
      return 0;
    }
  

  return Number(
     widthMm*thicknessMm*pitchMm*densityGCm3/1000*defectRatePct*(unitPrice-scrapUnitPrice*scrapRecycleRatePct)/1000+auxMaterialCost*defectRatePct
    ).toFixed(4);

})

watchEffect(() => {
  data.form.defectFee = defectFee.value;
});






const { queryParams, form, rules } = toRefs(data)

/** 查询冲压电镀件列表 */
function getList() {
  loading.value = true
  listPlating(queryParams.value).then(response => {
    platingList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pid: null,
    ptype: null,
    partNumber: null,
    partName: null,
    material: null,
    qtyPcs: null,
    unitPrice: null,
    scrapUnitPrice: null,
    thicknessMm: null,
    widthMm: null,
    pitchMm: null,
    densityGCm3: null,
    netWeightG: null,
    lossRatePct: null,
    defectRatePct: null,
    otherRemark: null,
    mgmtRatioPct: null,
    profitRatioPct: null,
    auxMaterialCost: null,
    scrapRecycleRatePct: null,
    punchesPerShot: null,
    materialConsumptionKg: null,
    defectFee: null,
    materialCost: null,
    mgmtFee: null,
    profitFee: null,
    totalMaterialCost: null,
    machineModelTonnage: null,
    costRatePct: null,
    punchSpeedSpm: null,
    minutePerHour: null,
    efficiencyPct: null,
    moldRepairDays: null,
    moldRepairCost: null,
    secondaryProcessFee: null,
    otherProcessRemark: null,
    processMgmtRatioPct: null,
    processProfitRatioPct: null,
    moldRepairRmbPcs: null,
    processDefectFee: null,
    punchCost: null,
    processMgmtFee: null,
    processProfitFee: null,
    totalPunchCost: null,
    stationName: null,
    laborHeadcount: null,
    cycleTimeS: null,
    cavitiesPcs: null,
    laborRateRmbH: null,
    laborTimeS: null,
    laborFee: null,
    packingFee: null,
    transportFee: null,
    totalCostBeforePlating: null,
    totalCostAfterPlating: null,
    punchCapacityPcsH: null,
    uid: null,
    updateDate: null,
    createDate: null,
    deleteDate: null
  }
  proxy.resetForm("platingRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加冲压电镀件"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPlating(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改冲压电镀件"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["platingRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePlating(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPlating(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除冲压电镀件编号为"' + _ids + '"的数据项？').then(function() {
    return delPlating(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/plating/export', {
    ...queryParams.value
  }, `plating_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<style scoped>
  .titleclass{
    font-family: "Inter", "PingFang SC", sans-serif;
    font-weight: 300;
    letter-spacing: .06em;
    color: var(--title-color);
    line-height: 1.2;
  }
  .calculate{
    margin-top: 20px;
    font-size: 14px;
  }
  .calculate .el-row {
    height: 50px;
  }
</style>